﻿#region License
// The MIT License (MIT)
// 
// Copyright (c) 2009 Christian Resma Helle
// 
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
// 
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
#endregion
using System;
using System.Globalization;
using System.Reflection;
using System.Text;
using System.Collections.Generic;

namespace ChristianHelle.DatabaseTools.SqlCe.CodeGenCore
{
    public abstract class CodeGenerator
    {
        protected readonly StringBuilder Code;

        protected CodeGenerator(ISqlCeDatabase database)
        {
            Database = database;
            CodeFiles = new Dictionary<string, StringBuilder>();
            Code = new StringBuilder();
            Code.AppendLine();
        }

        public ISqlCeDatabase Database { get; set; }
        public Dictionary<string, StringBuilder> CodeFiles { get; protected set; }

        public EntityGeneratorOptions EntityGeneratorOptions { get; set; }
        public DataAccessLayerGeneratorOptions DataAccessLayerGeneratorOptions { get; set; }

        public virtual void GenerateEntities() { }
        public virtual void GenerateDataAccessLayer() { }

        public virtual void WriteHeaderInformation()
        {
            WriteHeaderInformation(Code);
        }

        public void WriteHeaderInformation(StringBuilder code)
        {
            code.AppendLine("/*");
            code.AppendLine("\tThis code was generated by SQL Compact Code Generator version " + Assembly.GetCallingAssembly().GetName().Version);
            code.AppendLine();
            code.AppendLine("\tSQL Compact Code Generator was written by Christian Resma Helle (http://sqlcecodegen.codeplex.com)");
            code.AppendLine("\tand is under the The MIT License (MIT)");
            code.AppendLine();
            code.AppendLine("\tGenerated: " + DateTime.Now.ToString(CultureInfo.InvariantCulture));
            code.AppendLine("*/");
            code.AppendLine();
        }

        public virtual string GetCode()
        {
            return Code.ToString();
        }

        public virtual void ClearCode()
        {
            Code.Remove(0, Code.Length - 1);
        }

        protected static void GenerateXmlDoc(StringBuilder code, int tabPrefixCount, string summary, params KeyValuePair<string, string>[] parameters)
        {
            for (var i = 0; i < tabPrefixCount; i++)
                code.Append("\t");
            code.AppendLine("/// <summary>");

            for (var i = 0; i < tabPrefixCount; i++)
                code.Append("\t");
            code.AppendLine("/// " + summary);

            for (var i = 0; i < tabPrefixCount; i++)
                code.Append("\t");
            code.AppendLine("/// </summary>");

            foreach (var parameter in parameters)
            {
                for (var i = 0; i < tabPrefixCount; i++)
                    code.Append("\t");
                code.AppendFormat("/// <param name=\"{0}\">{1}</param>", parameter.Key, parameter.Value);
                code.AppendLine();
            }
        }

        protected void AppendCode(string className, StringBuilder code)
        {
            Code.Append(code);
            CodeFiles.Add(className, code);
        }
    }
}
